/*
 * The JAnalytics Library provides a Java API to the Google Analytics service.
 * Copyright (C) 2007 Ansir Development Limited (http://www.ansir.ca)
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

package ca.ansir.analytics.servlet;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 * Utility methods for servlets.
 * 
 * @author Dan Andrews
 */
public final class ServletUtilities {

	/**
	 * Constructor - do not construct.
	 */
	private ServletUtilities() {

	}

	/**
	 * Gets a parameter by name from the request object and if not found trys to
	 * obtain via the session attributes.
	 * 
	 * @param request
	 *            The <code>HttpServletRequest</code> request object.
	 * @param paramName
	 *            The name of the parameter/attribute.
	 * @param throwIfNull
	 *            If true this method will throw a <code>ServletException</code>
	 *            if the parameter value is null.
	 * @return The parameters value.
	 * @throws ServletException
	 */
	public static String getParameterByName(HttpServletRequest request,
			String paramName, boolean throwIfNull) throws ServletException {
		String paramValue = getParameterByName(request, paramName, null);
		if (throwIfNull && paramValue == null) {
			throw new ServletException(
					paramName
							+ " must not be null. It may be defind in as a request parameter or a session attribute");
		}
		return paramValue;
	}

	/**
	 * Gets a parameter by name from the request object and if not found trys to
	 * obtain via the session attributes.
	 * 
	 * @param request
	 *            The <code>HttpServletRequest</code> request object.
	 * @param paramName
	 *            The name of the parameter/attribute.
	 * @param defaultValue
	 *            The default value if the value is null.
	 * @return The parameters value.
	 */
	static String getParameterByName(HttpServletRequest request,
			String paramName, String defaultValue) {
		String paramValue = request.getParameter(paramName);
		if (paramValue == null) {
			paramValue = (String) request.getSession().getAttribute(paramName);
			if (paramValue == null && defaultValue != null) {
				paramValue = defaultValue;
			}
		}
		return paramValue;
	}

	/**
	 * Gets a parameter by name from the request object and if not found tries to
	 * obtain via the session attributes.
	 * 
	 * @param request
	 *            The <code>HttpServletRequest</code> request object.
	 * @param paramName
	 *            The name of the parameter/attribute.
	 * @param defaultValue
	 *            The default value if the parameter/attribute value is null.
	 * @return The integer value of the parameter/attributes value.
	 * @throws ServletException
	 */
	static int getIntegerParameterByName(HttpServletRequest request,
			String paramName, int defaultValue) throws ServletException {
		String paramValue = getParameterByName(request, paramName, Integer
				.toString(defaultValue));
		int rv = -1;
		try {
			if ("".equals(paramValue.trim())) {
				rv = defaultValue;
			} else {
				rv = Integer.parseInt(paramValue);
			}
		} catch (NumberFormatException e) {
			throw new ServletException(
					paramName
							+ " must be a valid integer '"
							+ paramValue
							+ "'. It may be defined in as a request parameter or a session attribute");
		}
		return rv;
	}

	/**
	 * Gets the <code>Calendar</code> from the date's string and given format.
	 * 
	 * @param dateString
	 *            The date's string representation.
	 * @param format
	 *            The date's format.
	 * @return The <code>Calendar</code> object representation.
	 * @throws ServletException
	 */
	static Calendar getCalendar(String dateString, String format)
			throws ServletException {
		Calendar calendar = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat(format);
		try {
			calendar.setTime(df.parse(dateString));
		} catch (ParseException e) {
			throw new ServletException("The date '" + dateString
					+ "' cannot be parsed using the format '" + format + "'", e);
		}
		return calendar;
	}

}
